package com.examp.config;

import com.examp.dao.DepartmentDao;
import com.examp.dao.EmployeeDao;
import com.examp.pojo.Department;
import com.examp.pojo.Employee;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.concurrent.Callable;

@Controller
public class EmployeeContoller {
    @Autowired
      EmployeeDao employeeDao;
    @Autowired
      DepartmentDao departmentDao;

    @RequestMapping("/emps")
      public String list(Model model){

          Collection<Employee> employees = employeeDao.getAll();
          model.addAttribute("emps",employees);
          return "emp/list";
      }
   @GetMapping("/emp")
    public String toAddpage(Model model) {

       //查处所有部门的信息
       Collection<Department> departments = departmentDao.getDepartments();
       model.addAttribute("departments", departments);
       return "emp/add";
   }

   @PostMapping("/emp")
       public String addEmp(Employee employee) {

       //保存员工信息
        employeeDao.save(employee);
       return "redirect:/emps";
    }

    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id, Model model){
        //查出原来的数据
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        //查处所有部门的信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments", departments);

        return "emp/update";
    }

    @PostMapping("/updateEmp")
    public String  UpdateEmp(Employee employee){

        employeeDao.save(employee);
        return "redirect:/emps";

    }

    //删除员工
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id ){
        employeeDao.delete(id);
        return "redirect:/emps";
    }




}
